package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogOperation;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.shiro.ShiroUtil;
import com.softer.wxzj.entity.SysUser;
import com.softer.wxzj.mapper.UserMapper;
import com.softer.wxzj.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * UserServiceImpl class
 * @author zcz
 * @date 2019/1/16
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public CommonResult insertUser(SysUser user) {
        //用户名是否存在
        Boolean exist = checkUserExist(user.getUserName());
        if(!exist){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "用户名已经存在");
        }
        String salt = ShiroUtil.createSalt();
        String passWord = ShiroUtil.createPassWord(getPassWord(user),salt);
        user.setSalt(salt);
        user.setPassWord(passWord);
        int i = userMapper.insert(user);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(SysUser user) {
        if(null != user && StringUtils.isNotBlank(user.getPassWord())){
            String salt = ShiroUtil.createSalt();
            String passWord = ShiroUtil.createPassWord(user.getPassWord(),salt);
            user.setSalt(salt);
            user.setPassWord(passWord);
        }
        int i = userMapper.update(user);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult delete(String id) {
        if(StringUtils.isBlank("id")){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        int i =  userMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<SysUser> getUserList(Page page, SysUser user) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysUser> list = userMapper.getList(user);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 密码重置
     * @param user
     * @return
     */
    @Override
    public CommonResult resetPassword(SysUser user) {
        String salt = ShiroUtil.createSalt();
        String passWord = ShiroUtil.createPassWord(getPassWord(user),salt);
        user.setSalt(salt);
        user.setPassWord(passWord);
        int i = userMapper.update(user);
        return CommonResult.ResponseResult(i);
    }

    /**
     * 检查用户是否存在
     * @return
     */
    public Boolean checkUserExist(String userName) {
        Boolean flag = true;
        int count = userMapper.getCountByUserName(userName);
        if (count>0){
            flag = false;
        }
        return flag;
    }

    /**
     * 设置初始密码
     * @param user
     * @return
     */
    public String getPassWord(SysUser user){
        if (null != user && StringUtils.isBlank(user.getPassWord())){
            return "123456";
        }
        return user.getPassWord();
    }
}
